﻿using AutoMapper;
using IntelligentAgriculture.Contracts.Dto.Input.ColumnManagemeninput;
using IntelligentAgriculture.Contracts.Dto.Input.ManageInput;
using IntelligentAgriculture.Contracts.Dto.Output.ColumnManagementOutput;
using IntelligentAgriculture.Contracts.Dto.Output.ManageOutput;
using IntelligentAgriculture.Contracts.Dto.Output.PageOutput;
using IntelligentAgriculture.Contracts.Interface.ColumnManagements;
using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Impl.Managements;
using IntelligentAgriculture.Domain.Interface.IColumnManagement;
using IntelligentAgriculture.Domain.Interface.IManagement;
using IntelligentAgriculture.Services.Management;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Services.ColumnManagementService
{
    public class ColumnManageService:IColumnManageService
    {
        private readonly IColumnManRepository _icolumnManRepository;
        private readonly ILogger<ColumnManageService> _logger;

        private readonly IMapper _mapper;
        public ColumnManageService(IColumnManRepository icolumnManRepository, ILogger<ColumnManageService> logger, IMapper mapper)
        {
            _icolumnManRepository = icolumnManRepository;
            _logger = logger;
            _mapper = mapper;
        }



        /// <summary>
        /// 添加功能
        /// </summary>
        /// <param name="entity">方法参数</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> Add(Columnmanagement entity)
        {
            try
            {
                var Addmanager = _mapper.Map<Columnmanagement>(entity);
                return await _icolumnManRepository.Add(Addmanager);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示，查询，分页功能
        /// </summary>
        /// <param name="input">查询参数</param>
        /// <returns>列表</returns>
        public PagesOutput<ColumnShowOutput> GetShow(ColumnShowinput input)
        {
            var list = _icolumnManRepository.GetShow().AsNoTracking();

            var result = new PagesOutput<ColumnShowOutput>()
            {
                Total = list.Count(),
                PageSize = input.PageSize
            };

            var query = list.OrderByDescending(x => x.ColumnManagementId).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize);
            result.Data = _mapper.Map<List<ColumnShowOutput>>(query);
            return result;
        }
		/// <summary>
		/// 根据栋舍获取栏位
		/// </summary>
		/// <returns></returns>
		public Task<IQueryable<Columnmanagement>> GetColumnManagement(int ridgepoleId)
		{
			try
			{
				var list = _icolumnManRepository.GetColumnManagement(ridgepoleId);
				return list;
			}
			catch (Exception ex)
			{
				_logger.LogError("获取栏位列表失败" + ex.Message);
				throw;
			}
		}
	}
}
